package com.atguigu.paymentdemo.service.impl;

import com.atguigu.paymentdemo.entity.OrderInfo;
import com.atguigu.paymentdemo.entity.RefundInfo;
import com.atguigu.paymentdemo.enums.OrderStatus;
import com.atguigu.paymentdemo.enums.wxpay.WxRefundStatus;
import com.atguigu.paymentdemo.mapper.RefundInfoMapper;
import com.atguigu.paymentdemo.service.OrderInfoService;
import com.atguigu.paymentdemo.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {
    @Resource
    OrderInfoService orderInfoService;
    @Resource
    Gson gson;

    /**
     * 创建初始退款日志
     * @param orderNo
     * @param reason
     */
    @Override
    public void createRefundInfo(String orderNo, String reason) {
        RefundInfo refundInfo = new RefundInfo();
        Integer totalfee =  orderInfoService.queryOrderTotalFeeByOrderNo(orderNo);
        refundInfo.setOrderNo(orderNo);
        refundInfo.setTotalFee(totalfee);
        refundInfo.setRefund(totalfee);
        refundInfo.setReason(reason);
        refundInfo.setRefundStatus(WxRefundStatus.PROCESSING.getType());
        baseMapper.insert(refundInfo);
    }

    /**
     * 1执行退款时，收到响应后更新数据

     *  @param bodyAsString

     */

    @Override
    public void updateRefundInfo(String bodyAsString,String orderNo){
        //*1.执行退款时，收到响应后更新数据
        //响应体解封装
        Map<String,Object> res = gson.fromJson(bodyAsString, HashMap.class);

         //根据订单编号，更新初始退款日志
        QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper();
         //"order_no"与数据库保持一致
        queryWrapper.eq("order_no",orderNo);
        RefundInfo refundInfo = this.getOne(queryWrapper);


        String refundNo = (String)res.get("out_refund_no");
        String refundStatus = (String)res.get("status");

        refundInfo.setRefundNo(refundNo);
        refundInfo.setRefundStatus(refundStatus);
        refundInfo.setContentReturn(bodyAsString);
        baseMapper.update(refundInfo,queryWrapper);
    }

    /**
     *
     * 收到退款回调后，更新数据
     *  @param plaintText

     */

    @Override
    public void updateRefundInfoByNotify(String plaintText,String orderNo){
        //*1.执行退款时，收到响应后更新数据
        //响应体解封装
        Map<String,Object> res = gson.fromJson(plaintText, HashMap.class);

        //根据订单编号，更新初始退款日志
        QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper();
        //"order_no"与数据库保持一致
        queryWrapper.eq("order_no",orderNo);
        RefundInfo refundInfo = this.getOne(queryWrapper);

        //退款回调后，更新退款信息
        refundInfo.setRefundId((String)res.get("refund_id"));
        refundInfo.setContentNotify(plaintText);
        refundInfo.setRefundStatus(WxRefundStatus.SUCCESS.getType());
        baseMapper.update(refundInfo,queryWrapper);

    }


    /**
     * 判断是否已处理
     * @param orderNo
     * @return
     */

    @Override
    public boolean isProcessedRefundInfo(String orderNo) {
        QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_no",orderNo);
        return this.getOne(queryWrapper).getRefundStatus().equals(WxRefundStatus.SUCCESS.getType());
    }

}
